package com.android.lib.download.utils

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken

val gson: Gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create() ?: throw Exception("gson初始化错误")

interface ToJson {
    fun toJson(): String = gson.toJson(this)
}

inline fun <reified T : ToJson> String.toDataClazz(): T = gson.fromJson(this, T::class.java)

fun <E> Iterable<E>.toJson(): String = gson.toJson(this.toList())

inline fun <reified E> String.toDataClazzList(): List<E> =
    gson.fromJson<List<E>>(this, object : TypeToken<List<E>>() {}.type)